﻿Public Class frmLogin

    Private m_selectedUser As Withings.User

    Public ReadOnly Property SelectedUser() As Withings.User
        Get
            Return m_selectedUser
        End Get
    End Property

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.lvUser.Items.Clear()
    End Sub
    Private Sub btnGetUserList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetUserList.Click
        Dim userAccount As Withings.Account
        Dim getUserResponse As Withings.GetUserListResponse

        Me.lblPleaseWait.Visible = True
        Application.DoEvents()

        userAccount = New Withings.Account(Me.txtEmailAddress.Text, Me.txtPassword.Text)
        getUserResponse = userAccount.GetUserList()

        If getUserResponse.Status = Core.StatusResponseEnum.Successfull Then
            Me.lvUser.Items.Clear()

            For Each item As Withings.User In getUserResponse.Users
                Dim isPublic As Boolean
                isPublic = (item.IsBloodPressureMonitorDataPublic Or item.IsBodyScaleDataPublic)

                Dim lvi As New ListViewItem()
                lvi.Text = item.LastName + ", " + item.FirstName + " - " + item.ShortName
                lvi.Tag = item

                If Not isPublic Then
                    lvi.Text += " - NOT PUBLIC"
                End If

                lvUser.Items.Add(lvi)

            Next
            Me.lblErrorMessage.Visible = False
        Else
            Me.lblErrorMessage.Text = getUserResponse.Status.ToString
            Me.lblErrorMessage.Visible = True
        End If

        Me.lblPleaseWait.Visible = False
    End Sub

    Private Sub lvUser_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvUser.DoubleClick
        If Me.lvUser.SelectedItems.Count = 0 Then Exit Sub
        Dim myUser As Withings.User

        myUser = CType(Me.lvUser.SelectedItems(0).Tag, User)
        If Not (myUser.IsBloodPressureMonitorDataPublic OrElse myUser.IsBodyScaleDataPublic) Then Exit Sub

        m_selectedUser = myUser
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

End Class